package me.leon.mylibs;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

import me.leon.devsuit.android.LogUtils;
import me.leon.mylibs.entity.TabEntity;
import me.leon.mylibs.widget.CircleIndicator;


public class MainActivity extends AppCompatActivity {

    private ViewPager pager;
    private SlidingTabLayout tab;
    private Context mContext = this;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<Fragment> mFragments2 = new ArrayList<>();
    private String[] mTitles = {"首页", "消息", "联系人", "更多"};
    private int[] mIconUnselectIds = {
            R.mipmap.tab_home_unselect, R.mipmap.tab_speech_unselect,
            R.mipmap.tab_contact_unselect, R.mipmap.tab_more_unselect};
    private int[] mIconSelectIds = {
            R.mipmap.tab_home_select, R.mipmap.tab_speech_select,
            R.mipmap.tab_contact_select, R.mipmap.tab_more_select};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private CommonTabLayout tab2;
    private CircleIndicator indicator;
    private TabLayout tabLayout;

    public static void start(Context context) {
        Intent starter = new Intent(context, MainActivity.class);
//        starter.putExtra();
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        for (String title : mTitles) {
//            mFragments.add(SimpleCardFragment.getInstance(title));
//        }
        tabLayout = ((TabLayout) findViewById(R.id.tablayout));
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }

        for (String title : mTitles) {
            mFragments.add(SimpleCardFragment.getInstance("Switch ViewPager " + title));
            mFragments2.add(SimpleCardFragment.getInstance("Switch Fragment " + title));
        }
        tab2 = (CommonTabLayout) findViewById(R.id.tl2);
        tab = (SlidingTabLayout) findViewById(R.id.tl);
        indicator = ((CircleIndicator) findViewById(R.id.indicator));
        pager = ((ViewPager) findViewById(R.id.pager));
        pager.setCurrentItem(2,false);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        indicator.setViewPager(pager);

       pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
           @Override
           public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

           }

           @Override
           public void onPageSelected(int position) {
                tab2.setCurrentTab(position);
           }

           @Override
           public void onPageScrollStateChanged(int state) {

           }
       });
        tab2.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                pager.setCurrentItem(position,false);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });


        tab2.setTabData(mTabEntities,this,R.id.fl_change,mFragments2);
        tab2.setCurrentTab(2);

        tab.setViewPager(pager);
        LogUtils.d("hello");
    }
    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
